package com.tjetc._4arrays;

import java.util.Arrays;
import java.util.Comparator;

public class _2ArraysExercise {
    public static void main(String[] args) {
        /*
            自定义Book类，里面包含name和price，按price排序(从大到小)。
            要求使用两种方式排序 , 有一个 Book[] books = 4本书对象.
            实现Comparator接口匿名内部类，也称为定制排序。
            可以按照 price (1)从大到小 (2)从小到大 (3) 按照书名长度从大到小

            思路分析：
            1. 定义一个类 name 和price
            2.两种排序

         */

        Book[] books = new Book[4];
        books[0] = new Book("Java", 10);
        books[1] = new Book("Python", 8);
        books[2] = new Book("C", 20);
        books[3] = new Book("C++", 18);

        //price 小----》大
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book book = (Book) o1;
                Book book1 = (Book) o2;
//                double difference = book.getPrice() - book1.getPrice();
                double difference = book.getName().length() -  book1.getName().length();
                if (difference > 0) {
                    return 1;
                } else if (difference < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });

        System.out.println(Arrays.toString(books));

    }
}

class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public Book() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
